
const path = require('path')

const Koa = require('koa')
const KoaStatic = require('koa-static')
// koaBody 和bodyParser 作用一样 都是用于解析body参数、但koaBody可用于上传
const { koaBody } = require('koa-body');
// const bodyParser = require('koa-bodyparser');

const parameter = require('koa-parameter');

const errHandler = require('../app/errHandler');

const app = new Koa()
// 文件信息挂载在 ctx.request.files上
app.use(koaBody({
    multipart: true,
    // 在option中 
    formidable: {
        uploadDir: path.join(__dirname, '../upload'),
        keepExtensions: true,

    },
    // 默认情况下 开启严格模式之后、koaBody只对'POST', 'PUT', 'PATCH' 进行参数绑定放到ctx.request.body中，配置他
    parsedMethods: ['POST', 'PUT', 'PATCH', 'DELETE']

}))
// 静态访问
app.use(KoaStatic(path.join(__dirname, '../upload')))
app.use(parameter(app));
// const userRouter = require('../router/user.route')
// const goodsRouter = require('../router/goods.route')
// app.use(userRouter.routes())
// app.use(goodsRouter.routes())
const router = require('../router')
app.use(router.routes())

// app.use(router.routes()).use(router.allowedMethods())
// 统一的错误处理
app.on('error', errHandler)

module.exports = app